package physics;

/**
 *
 * @author peter
 */
public class PVector {
	public float x;
	public float y;


	// GETTERS & SETTERS
	
	public float getX()
	{
		return x;
	}

	public float getY()
	{
		return y;
	}

	public void setX(float x)
	{
		this.x = x;
	}

	public void setY(float y)
	{
		this.y = y;
	}

	public void set(PVector other)
	{
		x = other.x;
		y = other.y;
	}


	// CONSTRUCTORS

	public PVector()
	{
	}

	public PVector(float x, float y)
	{
		this.x = x;
		this.y = y;
	}

	public PVector(PVector other)
	{
		this(other.x, other.y);
	}

	// METHODS

	public float dot(PVector other)
	{
		return x * other.getX() + y * other.getY();
	}

	public PVector negative()
	{
		return new PVector(-x, -y);
	}

	public void add(PVector vec)
	{
		x += vec.getX();
		y += vec.getY();
	}

	public void sub(PVector vec)
	{
		x -= vec.getX();
		y -= vec.getY();
	}

	public float getLength()
	{
		return (float) Math.sqrt(x * x + y * y);
	}

	public float getDistance(PVector vec)
	{
		float dx = vec.getX() - x;
		float dy = vec.getY() - y;
		return (float) Math.sqrt(dx*dx + dy*dy);
	}

	public void normalize()
	{
		float l = getLength();
		if (l == 0)
			return;
		x /= l;
		y /= l;
	}

	public String toString()
	{
		return "[ " + x + ", " + y + " ]" + "(" + getLength() + ")";
	}
}
